import 'package:flutter/material.dart';
import 'package:flutter_massage/common_lib/index.dart';
import 'package:flutter_massage/common_lib/routers/route.dart';
import 'package:flutter_massage/pages/web/web_logic.dart';
import 'package:flutter_massage/values/colors.dart';
import 'package:get/get.dart';
import 'package:webview_flutter/webview_flutter.dart';

class WebPage extends StatelessWidget {
  const WebPage(this.logic, {Key? key}) : super(key: key);

  final HtmlStringWebBaseLogic logic;

  @override
  Widget build(BuildContext context) {
    return BaseScaffold(
      titleStr: logic.getTitle(),
      child: GetBuilder<HtmlStringWebBaseLogic>(
        assignId: true,
        builder: (logic) {
          return Column(
            children: [
              Obx(() {
                if (logic.progressValue.value >= 100) return const SizedBox.shrink();
                return LinearProgressIndicator(
                  value: logic.progressValue.value.toDouble(),
                  backgroundColor: AppColors.white,
                  valueColor: const AlwaysStoppedAnimation(AppColors.primaryGreen),
                );
              }),
              Expanded(
                child: logic.controller == null ? const SizedBox() : WebViewWidget(controller: logic.controller!),
              ),
            ],
          );
        },
      ),
    );
  }
}

class WebH5Page extends GetView<WebH5Logic> {
  WebH5Page({
    Key? key,
    required this.url,
    this.title,
    this.showBack = true,
    this.needParam = true,
  }) : super(key: key);

  final String url;
  final String? title;
  final bool showBack;
  final bool needParam;

  final String _tag = DateTime.now().toString();

  @override
  String? get tag => _tag;

  @override
  Widget build(BuildContext context) {
    return GetBuilder<WebH5Logic>(
      tag: tag,
      init: WebH5Logic(url, needParam),
      assignId: true,
      builder: (logic) {
        return BaseScaffold(
          hideBackBtn: !showBack && logic.controller != null && !logic.canGoBack,
          titleStr: logic.title.getAuto(title.getAuto(' ')),
          onWillPop: () async {
            if (logic.canGoBack) {
              logic.controller?.goBack();
            } else {
              CommonRoute.close();
            }
          },
          actions: logic.controller != null
              ? [
                  CommonRootAction(
                    onTap: () => logic.controller!.reload(),
                    child: const Icon(Icons.refresh, color: Colors.black),
                  ),
                ]
              : null,
          child: Column(
            children: [
              Obx(() {
                if (logic.progressValue.value >= 100) return const SizedBox.shrink();
                return LinearProgressIndicator(
                  value: logic.progressValue.value.toDouble(),
                  backgroundColor: Colors.white,
                  valueColor: const AlwaysStoppedAnimation(AppColors.primaryGreen),
                );
              }),
              Expanded(
                child: logic.controller == null ? const SizedBox() : WebViewWidget(controller: logic.controller!),
              ),
            ],
          ),
        );
      },
    );
  }
}
